package com.uxhao.orderservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uxhao.commonutils.R;
import com.uxhao.commonutils.constants.ResultCode;
import com.uxhao.commonutils.util.JwtUtil;
import com.uxhao.orderservice.entity.OrderQuery;
import com.uxhao.orderservice.entity.dashboard.OrderAndAmountVo;
import com.uxhao.orderservice.entity.TOrder;
import com.uxhao.orderservice.entity.dashboard.WeekDataVo;
import com.uxhao.orderservice.service.TOrderService;
import com.uxhao.servicebase.exceptionhandler.QLLException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author uxhao
 * @since 2022/01/22
 */
@Api(tags = {"订单接口"})
@RestController
//@CrossOrigin
@RequestMapping("/orderservice/order")
public class TOrderController {

    @Autowired
    private TOrderService orderService;

    //根据课程id和用户id创建订单，返回订单id
    @ApiOperation(value = "生成订单")
    @PostMapping("createOrder/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request) {
        String id = JwtUtil.getMemberIdByJwtToken(request);//用户id
        if (StringUtils.isEmpty(id)) throw new QLLException(ResultCode.NOLOGIN.getCode(), "未登录");
        String orderId = orderService.saveOrder(courseId, JwtUtil.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);

    }

    @ApiOperation(value = "获取订单详情")
    @GetMapping("getOrder/{orderId}")
    public R get(@PathVariable String orderId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        TOrder order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

    @GetMapping("isBuyCourse/{memberid}/{id}")
    public boolean isBuyCourse(@PathVariable String memberid,
                               @PathVariable String id) {
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<TOrder>().eq("member_id", memberid).eq("course_id", id).eq("status", 1));

        return count > 0;
    }

    /**
     * [远程调用]获取总订单数和总交易金额
     *
     * @return
     */
    @ApiOperation(value = "[远程调用]获取总订单数和总交易金额")
    @GetMapping(value = "countAllOrderAndAmount")
    public Map<String, Object> countAllOrderAndAmount() {
        OrderAndAmountVo orderAndAmountVo = orderService.countAllOrderAndAmount();
        Map<String, Object> map = new HashMap<>();
        map.put("orderNumber", orderAndAmountVo.getOrderNumber());
        map.put("amount", orderAndAmountVo.getAmount());
        return map;
    }

    /**
     * [远程调用]获取上周和本周的订单情况，交易金额
     *
     * @return
     */
    @ApiOperation(value = "[远程调用]获取上周和本周的订单情况，交易金额")
    @GetMapping(value = "countLastAndCurrentOrderAndAmount")
    public Map<String, Object> countLastAndCurrentOrderAndAmount() {
        WeekDataVo lastWeekData = orderService.countLastOrderAndAmount();
        WeekDataVo currentWeekData = orderService.countCurrentOrderAndAmount();
        Map<String, Object> map = new HashMap<>();
        map.put("lastWeekData", lastWeekData);
        map.put("currentWeekData", currentWeekData);
        System.out.println(map);
        return map;
    }

    @ApiOperation(value = "条件分页查询订单")
    @PostMapping(value = "pageListOrderCondition/{current}/{limit}")
    public R pageListOrderCondition(@PathVariable long current,
                       @PathVariable long limit,
                       @RequestBody(required = false) OrderQuery orderQuery) {
        //创建page对象
        Page<TOrder> page = new Page<>(current, limit);
        //MP 构建条件
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        String name = orderQuery.getNickname();
        Integer status = orderQuery.getStatus();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        if (!StringUtils.isEmpty(name)) wrapper.like("nickname", name);
        if (!StringUtils.isEmpty(status)) wrapper.eq("status", status);//等于
        if (!StringUtils.isEmpty(begin)) wrapper.ge("gmt_create", begin);//大于
        if (!StringUtils.isEmpty(end)) wrapper.le("gmt_modified", end);//小于
        wrapper.orderByDesc("gmt_create");
        orderService.page(page, wrapper);
        long total = page.getTotal();//总记录数
        System.out.println(page.getTotal());
        List<TOrder> records = page.getRecords(); //数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

}

